import { defineStore } from 'pinia';
import { config } from '@/api/config';

export const useConfigStore = defineStore('config', {
  state: () => ({
    forbidStartTime: '',
    forbidEndTime: ''
  }),
  getters: {
    checkForbid() {
      if (!this.forbidStartTime || !this.forbidEndTime) {
        return false;
      }

      const now = new Date();
      const start = new Date(now.toDateString() + ' ' + this.forbidStartTime);
      const end = new Date(now.toDateString() + ' ' + this.forbidEndTime);

      // 处理结束时间早于开始时间的情况，跨天禁播
      if (end < start) {
        if (now >= start || now < end) {
          return true;
        }
      } else {
        if (now >= start && now < end) {
          return true;
        }
      }

      return false;
    }
  },
  actions: {
    getParams() {
      return new Promise((resolve, reject) => {
        config().then(res => {
          const { forbidStartTime, forbidEndTime } = res.data;
          this.forbidStartTime = forbidStartTime;
          this.forbidEndTime = forbidEndTime;
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
  }
});
